<?php
/**
 * @authors		sachin,shine,jonia
 * @copyright   CMM Technologies
 * @date        11-11-2011
 * @file		TMSFunctions.cls.php
 * @description manipulation of common functions are handled here.
 */

 class TMSFunctions extends Base
 {

	 /**
	 * generateSelect
	 *
     * A function to generate a select box
	 * given parameters like class, name etc.
	 *@param array $values Option names and values as associative array
	 *@param string $default - the option that is to be selected as default
	 *@param string $name name of the select -- optional
	 *@param string $class class of the select -- optional
	 *@param string $id id of the select -- optional
	 *@return string - returns the select box as string
	 */

	public static function generateSelect($names,$values,$default,$name="",$onChange="",$class="",$id="")
	 {
		parent::init();

		$valuesarr				=	explode(",",$values);
		$namesarr				=	explode(",",$names);

		if(count($valuesarr)   !=	count($namesarr))
			return "Error Occured";
	

		$output = "<select onChange='$onChange' name='$name' class='$class' id='$id'>".PHP_EOL;

		for($i=0;$i<count($valuesarr);$i++)
		{ 
			if($namesarr[$i]==$default)
				$output.="<option  selected='selected' value='$valuesarr[$i]'>$namesarr[$i]</option>".PHP_EOL;
			else
				$output.="<option  value='$valuesarr[$i]'>$namesarr[$i]</option>".PHP_EOL;
		}
		$output.="</select>";
		return $output;
	 }

	  public static function generateSelectFromDb($keyArray,$tableName,$primaryKey,$displayColumn,$valueColumn,$default,$topName,$name="",$onChange="",$class="",$id="")
	 {
		 parent::init();
		 $names=$topName;
		 $values=$topName;
		

		 foreach($keyArray as $key)
		 {
			 $cond = "$primaryKey='$key'";

			 $dataArr = self::$db->getDBContentsByCond($tableName,$cond);

			
			$values.=",";
			$names.=",";
			

			 $values.=$dataArr[0][$valueColumn];
			 $names.=$dataArr[0][$displayColumn];
			 
			 $first=false;
		 }

		 return self::generateSelect($names,$values,$default,$name,$onChange,$class,$id);


	 }

	
	/**
	 * Function to get designation from the user id
	 * @param int $id 
	 * @return designation of the user
	 */
	 public static  function getDesignationFromId($id)
	 {
		parent::init();
		$result=self::$db-> getCellContentByCondition("tms_designation","designation","id=$id");
		return $result;
	 }
	/**
	 * Function to get username from user id 
	 * @param int $id 
	 * @return username of the user
	 */
	  public static  function getUsernameFromId($id)
	 {
		parent::init();
		$result=self::$db->getCellContentByCondition("tms_users","username","id=$id");
		return $result;
	 }
	  
	/**
	 * Function to get module name from  user id 
	 * @param int $id 
	 * @return module name 
	 */ 
	   public  static function  getModuleNameFromId($id)
	 {
		parent::init();
		$result=self::$db->getCellContentByCondition("tms_project_modules","module_name","id='$id'");
		return $result;
	 }

	/**
	 * Function to get id from username
	 * @param string $username 
	 * @return id of the user
	 */

	   public static  function getIdFromUsername($username)
	 {
		parent::init();
		$result=self::$db->getCellContentByCondition("tms_users","id","username='$username'");
		return $result;
	 }

	    public static  function getDesignations()
	 {
		parent::init();
		$result=self::$db->getDBFieldByCond("tms_designation","id","true");
		return $result;
	 }
	     public static  function getProjectIDFromModuleID($id)
	 {
		parent::init();
		$result=self::$db->getCellContentByCondition("tms_project_modules","projects_id","id='$id'");
		return $result;
	 }


 }